﻿using System;
using System.Reflection;
using System.Web.Mvc;

using Ninject.Core;
using Ninject.Conditions;

using BlueSky.Web.Extentions;

using BlueSky.Web.IoC;

namespace BlueSky.Web.IoC.Ninject
{
    /// <summary>
    /// A module that automatically loads and registers all MVC controllers in specific assemblies.
    /// </summary>
    public class AutoControllerModule : StandardModule, IAutoBinder
    {
        protected readonly Assembly _assembly;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="assembly">Specific assembly.</param>
        public AutoControllerModule(Assembly assembly)
        {
            _assembly = assembly;
        }

        public override void Load()
        {
            AutoBinderBase.BindAll(_assembly, this);
        }
        
        public bool Filter(Type type)
        {            
            return typeof(IController).IsAssignableFrom(type);
        }

        public void Binder(Type type)
        {
            Bind<IController>().To(type).Only(When.Context.AsController(type));
        }
    }
}